import type { ConfigEnv, UserConfig } from 'vite';
import { loadEnv,defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import dayjs from 'dayjs';
import {join} from 'path'

import vueJsx from "@vitejs/plugin-vue-jsx";

/* eslint-disable import/no-unresolved */
import AutoImport from 'unplugin-auto-import/vite';

// antdv4.0版本引用文件
import { theme } from 'ant-design-vue/lib';
import AntdToken from 'ant-design-vue/lib/theme/convertLegacyToken';
const { defaultAlgorithm, defaultSeed } = theme;

const mapToken = defaultAlgorithm(defaultSeed);
const v3Token = AntdToken['default'](mapToken);

//构建打包文件
const buildAssetsDir = () =>{
    return dayjs().format('MMDDHHmm');
};

export default defineConfig(({mode}:ConfigEnv):UserConfig=>{
    const root = process.cwd();
    const env = loadEnv(mode,root)
    const assetsDir = buildAssetsDir();
    return  {
        base: env.VITE_APP_PUBLIC_PATH,
        plugins:[
            vue(),
            vueJsx(),
            AutoImport({
                dts: 'types/auto-imports.d.ts', // 这里是生成的global函数文件
                imports: ['vue'], // 需要自动导入的插件
                // 解决eslint报错问题
                // eslintrc: {
                //     // 这里先设置成true, 运行之后会生成 .eslintrc-auto-import.json 文件之后，在改为false
                //     enabled: false,
                //     filepath: './.eslintrc-auto-import.json', // 生成的文件路径
                //     globalsPropValue: true,
                // },
            }),
        ],
        build:{
            assetsDir: assetsDir,
            chunkSizeWarningLimit: 2048,
            cssCodeSplit: false,
            outDir: env.VITE_OUT_DIR,
            rollupOptions: {
              output: {
                manualChunks: {
                  vue: ['vue', 'pinia', 'vue-router'],
                  antd: ['ant-design-vue', '@ant-design/icons-vue'],
                },
              },
            },
        },
        esbuild:{
            //移除console
            drop: mode==='production'?['console','debugger']:[],
        },
        resolve: {
            alias: {
                '@': join(__dirname, './src'),
            },
        },
        css: {
            preprocessorOptions: {
                less: {
                    modifyVars: v3Token, // antdv4.0版本使用

                    // DO NOT REMOVE THIS LINE
                    javascriptEnabled: true,
                },
            },
        },
        server: {
            // host: true,
            host: '192.168.8.90',
            port: 2080,
            proxy:{
                '/apis': {
                    // backend url
                    target: env.VITE_APIS_WEB_HOST,
                    ws: false,
                    changeOrigin: true,
                },
            },
        },
    }
});
